package com.leetcode.questiononehundardandfifty.哈希表;

import java.nio.charset.CharacterCodingException;
import java.util.*;

/**
 * @author wtcode
 * @Description
 * @date 2025/3/27 - 9:18
 */
public class Answer {

    public static void main(String[] args) {

        int[] nums = new int[]{100,4,200,1,3,2};
        longestConsecutive(nums);


    }
    // 赎金信
    public boolean canConstruct(String ransomNote, String magazine) {
        HashMap<Character, Integer> map = new HashMap();
        for (int i = 0; i < magazine.length(); i++) {
            if(map.containsKey(magazine.charAt(i))){
                map.put(magazine.charAt(i),map.get(magazine.charAt(i))+1);
            }else {
                map.put(magazine.charAt(i),1);
            }
        }

        for (int i = 0; i < ransomNote.length(); i++) {
            char c = ransomNote.charAt(i);
            if(map.containsKey(c)){
                Integer nums = map.get(c);
                if(nums > 0){
                    nums--;
                    map.put(c,nums);
                }else {
                    return false;
                }
            }else {
                return false;
            }
        }


        return true;

    }

    // 同构字符串
    public boolean isIsomorphic(String s, String t) {
        HashMap<Character,Character> sMap = new HashMap();
        HashMap<Character,Character> tMap = new HashMap();
        for (int i = 0; i < s.length(); i++) {
            char a = s.charAt(i);
            char b = t.charAt(i);

            if(sMap.containsKey(a) && sMap.get(a) != b || tMap.containsKey(b) && tMap.get(b) != a){
                return false;
            }

            sMap.put(a,b);
            tMap.put(b,a);
        }
        return true;
    }

    // 单词规律
    public boolean wordPattern(String pattern, String s) {
        HashMap<Character,String> pMap = new HashMap();
        HashMap<String,Character> sMap = new HashMap();

        String[] words = s.split(" ");
        if(pattern.length() != words.length){
            return false;
        }

        for (int i = 0; i < pattern.length(); i++) {
            char p = pattern.charAt(i);

            if(pMap.containsKey(p) && !pMap.get(p).equals(words[i]) || sMap.containsKey(words[i]) && sMap.get(words[i]) != p){
                return false;
            }

            pMap.put(p,words[i]);
            sMap.put(words[i],p);

        }

        return true;
    }

    // 有效的字母异位词
    public boolean isAnagram(String s, String t) {

        if(s.length() != t.length()){
            return false;
        }

        int[] chars = new int[26];
        for (int i = 0; i < s.length(); i++) {
            chars[s.charAt(i) - 'a']++;
        }
        for (int i = 0; i < t.length(); i++) {
            chars[t.charAt(i) - 'a']--;
            if(chars[t.charAt(i) - 'a'] < 0){
                return false;
            }
        }

        return true;

    }


    // 字母异位词分组
    public List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> res = new ArrayList();
        Map<String, List<String>> map = new HashMap();

        for (String str : strs) {
            char[] chars = str.toCharArray();
            Arrays.sort(chars);
            String sortStr = new String(chars);
            if(map.containsKey(sortStr)){
                List<String> strings = map.get(sortStr);
                strings.add(str);
            }else {
                List<String> strings = new ArrayList();
                strings.add(str);
                map.put(sortStr,strings);
            }
        }
        for (List<String> value : map.values()) {
            res.add(value);
        }
        return res;
    }

    // 两数之和
    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap();
        for (int i = 0; i < nums.length; i++) {
            if(map.containsKey(target-nums[i])){
                return new int[]{map.get(target-nums[i]),i};
            }

            map.put(nums[i],i);
        }
        return new int[]{-1,-1};
    }

    // 快乐数
    public static boolean isHappy(int n) {
        int slow = n;
        int fast = getNext(n);

        while (fast != 1 && slow != fast){
            slow = getNext(slow);
            fast = getNext(getNext(fast));
        }
        return fast == 1;
    }

    public static int getNext(int n){
        int totalNum = 0;
        while (n > 0){
            int d = n % 10;
            n = n / 10;
            totalNum += d * d;
        }
        return totalNum;
    }

    // 存在重复元素
    public static boolean containsNearbyDuplicate(int[] nums, int k) {
        HashMap<Integer,Integer> map = new HashMap();
        for (int i = 0; i < nums.length; i++) {
            if(map.containsKey(nums[i])){
                Integer j = map.get(nums[i]);
                if(Math.abs(j-i) <= k){
                    return true;
                }else {
                    map.put(nums[i],i);
                }
            }else {
                map.put(nums[i],i);
            }

        }
        return false;
    }

    // 最长连续序列
    public static int longestConsecutive(int[] nums) {
        int ans = 0;
        HashSet<Integer> set = new HashSet();
        for (int num : nums) {
            set.add(num);
        }
        for (Integer x : set) {
            if(set.contains(x-1)){
                continue;
            }

            int y = x + 1;
            while (set.contains(y)){
                y++;
            }
            ans = Math.max(ans,y-x);

        }
        return ans;
    }

}
